package com.dgut.staffmodule.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.staffmodule.dao.DepartmentRepository;
import com.dgut.staffmodule.dao.PositionRepository;
import com.dgut.staffmodule.dao.StaffRepository;
import com.dgut.staffmodule.entity.Department;
import com.dgut.staffmodule.entity.DepartmentEditDTO;
import com.dgut.staffmodule.entity.Position;
import com.dgut.staffmodule.entity.Staff;

@Service
@Transactional
public class DepartmentService implements IDepartmentService {
	@Autowired
	private DepartmentRepository departmentRepository;// 部门dao
	@Autowired
	private StaffRepository staffRepository;// 员工dao
	@Autowired
	private PositionRepository positionRepository;// 职位dao

	// 保存部门
	public void saveOrUpdate(Department department) {
		departmentRepository.save(department);
	}

	// 删除部门
	public boolean delete(Department department) {
		// 如果该部门下不存在职位与员工才可删除部门
		List<Position> positions = positionRepository.findAllByDeptId(department.getId());
		List<Staff> staffs = staffRepository.findByDeptId2(department.getId());
		if (positions.isEmpty() && staffs.isEmpty()) {
			departmentRepository.delete(department);
			return true;
		} else {
			return false;
		}
	}

	// 批量删除部门
	public boolean delete(List<Long> ids) {
		// 如果该部门下不存在职位与员工才可删除部门
		List<Department> departments = (List<Department>) departmentRepository.findAll(ids);
		List<Position> positions = new ArrayList<Position>();
		List<Staff> staffs = new ArrayList<Staff>();
		for (Department d : departments) {
			positions = positionRepository.findAllByDeptId(d.getId());
			staffs = staffRepository.findByDeptId2(d.getId());
		}
		if (positions.isEmpty() && staffs.isEmpty()) {
			departmentRepository.delete(departments);
			return true;
		} else {
			return false;
		}
	}

	// 查找一个部门
	@Transactional(readOnly = true)
	public Department findOne(Long id) {
		return departmentRepository.findOne(id);
	}

	// 查找所有部门
	@Transactional(readOnly = true)
	public List<Department> findAll() {
		List<Department> deptLists = (List<Department>) departmentRepository.findAll();
		for (Department d : deptLists) {
			int count = staffRepository.findByDeptId(d.getId());
			d.setCount(count);
			System.out.println(d.getCount());
		}
		return deptLists;
	}

	// 分页查找所有部门
	@Override
	public Page<Department> findAll(Specification<Department> spec, Pageable pageable) {
		Page<Department> page = departmentRepository.findAll(spec, pageable);
		return page;
	}

	// 保存前端传的DTO
	public void save(DepartmentEditDTO departmentEditDTO) {
		Department department = new Department();
		department.setDeptName(departmentEditDTO.getDeptName());
		department.setSetupDate(departmentEditDTO.getSetupDate());
		departmentRepository.save(department);

	}

	// 更新前端传的DTO
	@Override
	public void update(DepartmentEditDTO departmentEditDTO) {
		Department department = departmentRepository.findOne(departmentEditDTO.getId());
		List<Staff> staffs = staffRepository.findByDeptId2(department.getId());
		List<Position> positions = positionRepository.findAllByDeptId(departmentEditDTO.getId());
		for (Staff staff : staffs) {
			staff.getDepartment().setDeptName(departmentEditDTO.getDeptName());
			System.out.println(staff);
		}
		for (Position p : positions) {
			p.getDepartment().setDeptName(departmentEditDTO.getDeptName());
			System.out.println(p);
		}
		department.setDeptName(departmentEditDTO.getDeptName());
		department.setSetupDate(departmentEditDTO.getSetupDate());
		departmentRepository.save(department);
	}

	// 通过部门名称查找部门
	@Override
	public Department findByDeptName(String department) {
		return departmentRepository.findByDeptName(department);
	}

	// 查找部门领导，流程查找任务的时候用到
	@Override
	public Department findDeptAudit(Long id) {
		Department department = departmentRepository.findDeptAudit(id);
		return department;
	}

	/*
	 * @Override public int addCount(Long id) { int count =
	 * departmentRepository.addCount(id); return count; }
	 * 
	 * @Override public int descCount(Long id) { int count =
	 * departmentRepository.descCount(id); return count; }
	 * 
	 * @Override public int descCount(Long id, int size) { int count =
	 * departmentRepository.descCount(id, size); return count; }
	 */
}
